package proves;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
 
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import fuse.LaCollaFS.Metadades.Directori;
import fuse.LaCollaFS.Metadades.Fitxer;
import fuse.LaCollaFS.Metadades.MetaDades;
import fuse.compat.FuseDirEnt;
import fuse.compat.FuseStat;


public class prova {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws JDOMException 
	 * @throws FileNotFoundException 
	 * @throws ParserConfigurationException
	 * @throws IOException
	 * @throws SAXException
	 */
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws FileNotFoundException, JDOMException, IOException {
		SAXBuilder builder = new SAXBuilder ();
		Document doc = builder.build (new FileInputStream ("test/proves/hola.xml"));
		Element root = doc.getRootElement ();
	//	p(root.getName ());                            // devuelve "tienda"
		//p(root.getAttributeValue ("nombre"));          // devuelve "tienda para geeks"
	//	p(root.getAttributeValue ("permisos"));       // devuelve "Tokio, Japon"
		List l = root.getChildren ();  
		
		for (int i = 0; i < l.size(); i++) {
			Element node;
			node =(Element) l.get(i);
	//		p(node.getName());
	//		p(node.getAttributeValue("nombre"));
					
		}
		
		p("ara crearem un xml........................");
		
		FuseStat estat;
				
		Element root2 = new Element ("Directori");
		root2.setAttribute ("Nom", "/");
		root2.setAttribute ("Pare", "/media/");
		
		Element item1 = new Element ("Directori");
		item1.setAttribute ("Nom", "hola");
		item1.setAttribute ("Pare", "/");
		root2.addContent (item1);
//		 Realizamos lo mismo con los elementos restantes
		XMLOutputter outputter = new XMLOutputter ("",true);
		try{outputter.output (new Document(root2),new FileOutputStream ("test/proves/hola2.xml"));
		   } catch (Exception e){
		    e.getMessage();
		} 
		MetaDades dades = null;   
		Directori dir = new Directori("/",dades);  
		Directori dir2 = new Directori("hola",dades);
		Directori dir3 = new Directori("adeu",dades);
		Fitxer fit1 = new Fitxer("readme.txt",dades);
		Fitxer fit2 = new Fitxer("adasd.txt",dades);
		Fitxer fit3 = new Fitxer("adeu.txt",dades);
		p(fit1.getNom());
		dir2.AfegirFill(fit2);
		dir.AfegirFill(dir2);
		dir.AfegirFill(dir3);
		dir.AfegirFill(fit1);
		dir.AfegirFill(fit3);
		
		Element rootdir = dir.CrearXML();
		p("xml creat");
		p("xml creat, anem a escriure'l.....................");
		XMLOutputter outputter2 = new XMLOutputter("", true);
		try {
			outputter2.output(new Document(rootdir), new FileOutputStream(
					"test/proves/dir.xml"));
		} catch (Exception e) {
			e.getMessage();
		} 
		 
		   p("llegint i interpretant el XML......");
		   
			SAXBuilder builder2 = new SAXBuilder ();
			Document doc2 = builder2.build (new FileInputStream ("test/proves/dir.xml"));
			Element rootl = doc2.getRootElement ();
			Directori dirllegit = new Directori("",dades);
			dirllegit.LlegirXML(rootl);
			
			p("Nom del directori Arrel : "+dirllegit.getNom());
			FuseDirEnt[] dirent = dirllegit.Llistarfills();
			Directori dirnobuit = (Directori) dirllegit.BuscarFill("/hola");
			Directori dirbuit = (Directori) dirllegit.BuscarFill("/adeu");
			p(dirnobuit.Buit());
			p(dirbuit.Buit());
			for (int i = 0; i < dirent.length; i++) {
				p(dirent[i].name);
			
					
			}
			
		   
		   
		   
		   
	}

	private static void p(Object o) {
		System.out.println(o); // TODO Auto-generated method stub

	}

}
